<template>
    <div>
      <mt-header fixed title="新闻列表">
        <router-link to="/" slot="left">
          <mt-button icon="back" @click="$router.back(-1)">返回</mt-button>
        </router-link>
      </mt-header>
      <div class="contents">
        <ul
          v-infinite-scroll="loadMore"
          infinite-scroll-disabled="loading"
          infinite-scroll-distance="10">
          <li v-for="(item,index) in list" :Key="index" @click="jumpPage(item.news_id)">
            <div class="list-img">
              <img :src="item.firstImg" alt="">
            </div>
            <div class="list-title">
              <div class="list-title-t">{{item.title}}</div>
              <div class="list-title-b">来源：{{item.source}}</div>
            </div>
          </li>
        </ul>
        <div class="more">{{more}}</div>
      </div>

    </div>
</template>

<script>
export default {
  name: 'NewsList',
  data () {
    return {
      list:[],
      page:1,
      loading:false,
      more:''
    }
  },
  created(){
  
  },
  methods:{
    loadMore() {
      this.requestData();
    },
    requestData(){
      this.$axios.get('getNewsList',{
        params: {
          page: this.page,
          pageNum:20
        }
      })
      .then(res=>{
        this.loading=true;//先关闭
        if(this.page > res.data.data.last_page){
          this.loading=true;//当前页数等于最后页数则关闭刷新
          this.more="没有更多";
        }else{
          this.loading=false;//打开刷新
          this.list=this.list.concat(res.data.data.data);
          this.page=this.page+1;
          this.more="加载中...";
        }
      })
      .catch(err=>{
        console.log(err);
      });
    },
    jumpPage(news_id){
      this.$router.push({  //核心语句
        path:'/NewsList/NewsPage',   //跳转的路径
        query:{           //路由传参时push和query搭配使用 ，作用时传递参数
          news_id:news_id 
        }
      })
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
  .contents{
    margin-top: 40px;
  }
  .contents ul{
    padding:0 10px;
  }
  .contents ul li{
    display: flex;
    height: 80px;
    padding:5px 0;
    list-style: none;
    overflow: hidden;
    border-bottom: 1px solid #eee;
  }
  .contents ul li img{
    width: 80px;
    height: 80px;
  }
  .list-title{
    padding: 10px;
  }
  .list-title-t{
    height: 40px;
    overflow: hidden;
    font-size: 14px;
  }
  .list-title-b{
    height: 40px;
    line-height: 40px;
    color: #555;
    font-size: 12px;
  }
  .more{
    text-align: center;
    font-size: 14px;
    color: #555;
  }
</style>
